package cz.worsik.android.test.rideshare.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cz.worsik.android.test.rideshare.R;
import cz.worsik.android.test.rideshare.model.Route;
import cz.worsik.android.test.rideshare.model.RouteSpinnerItem;

/**
 * Created by 231260 on 29.5.14.
 */
public class RouteListAdapter extends ArrayAdapter<Route> {

    private final List<Route> list;
    private final Activity context;

    private final static int resource = R.layout.route_list_item;
    private final static int res_name = R.id.route_name;
    private final static int res_type = R.id.route_type;
    private final static int res_icon = R.id.route_icon;

    public RouteListAdapter(Activity context, List<Route> list) {
        super(context, resource, list);
        this.list = list;
        this.context = context;
    }

    static class ViewHolder {
        protected ImageView icon_v;
        protected TextView name_v;
        protected TextView type_v;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(resource, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.name_v = (TextView) view.findViewById(res_name);
            viewHolder.type_v = (TextView) view.findViewById(res_type);
            viewHolder.icon_v = (ImageView) view.findViewById(res_icon);
            view.setTag(viewHolder);
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.name_v.setText(list.get(position).getName());
        int resId = context.getResources().getIdentifier(
            list.get(position).getType().getValue(),
            "string",
            context.getPackageName()
        );
        holder.type_v.setText(context.getString(resId));
        //holder.icon_v.setImageDrawable(null);
        return view;
    }
}
